package com.milhouz.quizzdroid.bean;

import android.os.Parcel;
import android.os.Parcelable;

public class Game implements Parcelable {

	public static final int NB_TEAM_DEFAULT = 1;

	GameMode gameMode;
	boolean isSoundEnable = false;
	boolean isTextToSpeech = false;
	boolean isCountDown = false;
	long countDownTime = 0;

	int nbPlayers = 1;
	Player[] players = new Player[] { new Player(nbPlayers) };

	private int currentPlayerIndex = 0;

	public enum GameMode {
		SINGLE, MULTIPLAYER
	}

	public Game(final GameMode gameMode) {
		this.gameMode = gameMode;
	}

	public Game(final GameMode gameMode, final boolean isTextToSpeech, final boolean isCountDown, final long countDownTime) {
		this.gameMode = gameMode;
		this.isTextToSpeech = isTextToSpeech;
		this.isCountDown = isCountDown;
		this.countDownTime = countDownTime;
		initPlayers();
	}

	public Game(final GameMode gameMode, final boolean isTextToSpeech, final boolean isCountDown, final long countDownTime, final int nbPlayers) {
		this.gameMode = gameMode;
		this.isTextToSpeech = isTextToSpeech;
		this.isCountDown = isCountDown;
		this.countDownTime = countDownTime;
		this.nbPlayers = nbPlayers;
		initPlayers();
	}

	public void initPlayers() {
		players = new Player[this.nbPlayers];
		for (int i = 0; i < nbPlayers; i++) {
			this.players[i] = new Player(i + 1);
		}
	}

	public void setNextPlayer() {
		this.currentPlayerIndex++;
		if (this.currentPlayerIndex >= players.length) {
			this.currentPlayerIndex = 0;
		}
	}

	public Player getCurrentPlayer() {
		return players[this.currentPlayerIndex];
	}

	public GameMode getGameMode() {
		return gameMode;
	}

	public boolean isTextToSpeech() {
		return isTextToSpeech;
	}

	public boolean isCountDown() {
		return isCountDown;
	}

	public long getCountDownTime() {
		return countDownTime;
	}

	public int getNbPlayers() {
		return nbPlayers;
	}

	public boolean isSoundEnable() {
		return isSoundEnable;
	}

	public void setSoundEnable(boolean isSoundEnable) {
		this.isSoundEnable = isSoundEnable;
	}

	public void setNbPlayers(int nbPlayers) {
		this.nbPlayers = nbPlayers;
	}

	public void setCurrentPlayer(int currentPlayer) {
		this.currentPlayerIndex = currentPlayer;
	}

	public Player[] getPlayers() {
		return players;
	}

	@Override
	public void writeToParcel(Parcel parcel, int flags) {
		parcel.writeString(gameMode == null ? null : gameMode.name());
		parcel.writeString(String.valueOf(isTextToSpeech));
		parcel.writeString(String.valueOf(isCountDown));
		parcel.writeLong(countDownTime);
		parcel.writeInt(nbPlayers);
	}

	public static final Creator<Game> CREATOR = new Creator<Game>() {
		@Override
		public Game createFromParcel(Parcel source) {
			GameMode gameMode = GameMode.valueOf(source.readString());
			boolean isTextToSpeech = Boolean.valueOf(source.readString());
			boolean isCountDown = Boolean.valueOf(source.readString());
			long countDownTime = source.readLong();
			int nbTeams = source.readInt();
			Game e = new Game(gameMode, isTextToSpeech, isCountDown, countDownTime, nbTeams);
			return e;
		}

		@Override
		public Game[] newArray(int arg0) {
			return null;
		}
	};

	@Override
	public int describeContents() {
		return 0;
	}

	public void setTextToSpeech(boolean isTextToSpeech) {
		this.isTextToSpeech = isTextToSpeech;
	}

	public void setCountDown(boolean isCountDown) {
		this.isCountDown = isCountDown;
	}

	public void setCountDownTime(long countDownTime) {
		this.countDownTime = countDownTime;
	}

	public void setNbTeams(int nbTeams) {
		this.nbPlayers = nbTeams;
	}

}